package com.pcafs.manage.controller;

import java.io.File;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.pcafs.common.annotation.Log;
import com.pcafs.common.core.controller.BaseController;
import com.pcafs.common.core.domain.AjaxResult;
import com.pcafs.common.core.domain.entity.SysUser;
import com.pcafs.common.core.domain.model.LoginUser;
import com.pcafs.common.core.page.TableDataInfo;
import com.pcafs.common.enums.BusinessType;
import com.pcafs.common.utils.ServletUtils;
import com.pcafs.common.utils.poi.ExcelUtil;
import com.pcafs.manage.domain.PcafsField;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pcafs.manage.vo.PcafsFieldVo;
import com.pcafs.manage.bo.PcafsFieldQueryBo;
import com.pcafs.manage.bo.PcafsFieldAddBo;
import com.pcafs.manage.bo.PcafsFieldEditBo;
import com.pcafs.manage.service.IPcafsFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

/**
 * 字段信息Controller
 * 
 * @author pcafs
 * @date 2022-05-05
 */
@Api(value = "字段信息控制器", tags = {"字段信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/manage/field")
public class PcafsFieldController extends BaseController {

    private final IPcafsFieldService iPcafsFieldService;

    /**
     * 查询字段信息列表
     */
    @ApiOperation("查询字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:list')")
    @GetMapping("/list")
    public TableDataInfo<PcafsFieldVo> list(PcafsFieldQueryBo bo) {
        startPage();
        List<PcafsFieldVo> list = iPcafsFieldService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 查询字段信息列表
     */
    @ApiOperation("查询字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:list')")
    @GetMapping("/getList")
    public TableDataInfo<PcafsField> getList(PcafsFieldQueryBo bo) {
        startPage();
        List<PcafsField> list = iPcafsFieldService.queryAll(bo);
        return getDataTable(list);
    }

    /**
     * 查询字段信息列表
     */
    @ApiOperation("查询字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:list')")
    @GetMapping("/getFieldList/{tabNameEn}")
    public AjaxResult<List> getFieldList(@PathVariable("tabNameEn") String tabNameEn) {
        return AjaxResult.success(iPcafsFieldService.getFieldList(tabNameEn));
    }

    @Log(title = "数据采集", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        return AjaxResult.success(iPcafsFieldService.importData(file));
    }

    @PostMapping("/importDataToTab")
    public AjaxResult importDataToTab(Map map) {
        return AjaxResult.success(iPcafsFieldService.importDataToTab(map));
    }

    /**
     * 查询字段信息和数据
     */
    @ApiOperation("查询字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:list')")
    @GetMapping("/getFieldAndData")
    public AjaxResult<Map> getFieldAndData(String tabNameEn) {
        Map<String, Object> map = iPcafsFieldService.getFieldAndData(tabNameEn);
        return AjaxResult.success(map);
    }

    /**
     * 查询字段信息和数据
     */
    @ApiOperation("查询字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:list')")
    @GetMapping("/getTabData")
    public TableDataInfo<Map> getTabData(PcafsField bo) {
        startPage();
        List<Map> list = iPcafsFieldService.getTabData(bo.getTabNameEn());
        return getDataTable(list);
    }

    /**
     * 导出字段信息列表
     */
    @ApiOperation("导出字段信息列表")
//    @PreAuthorize("@ss.hasPermi('manage:field:export')")
    @Log(title = "字段信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PcafsFieldVo> export(PcafsFieldQueryBo bo) {
        List<PcafsFieldVo> list = iPcafsFieldService.queryList(bo);
        ExcelUtil<PcafsFieldVo> util = new ExcelUtil<PcafsFieldVo>(PcafsFieldVo.class);
        return util.exportExcel(list, "字段信息");
    }

    /**
     * 获取字段信息详细信息
     */
    @ApiOperation("获取字段信息详细信息")
//    @PreAuthorize("@ss.hasPermi('manage:field:query')")
    @GetMapping("/{id}")
    public AjaxResult<PcafsFieldVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iPcafsFieldService.queryById(id));
    }

    @ApiOperation("获取字段信息详细信息")
//    @PreAuthorize("@ss.hasPermi('manage:field:query')")
    @GetMapping("/haveData/{tabNameEn}")
    public AjaxResult<Integer> haveData(@PathVariable("tabNameEn" ) String tabNameEn) {
        return AjaxResult.success(iPcafsFieldService.haveData(tabNameEn));
    }

    /**
     * 新增字段信息
     */
    @ApiOperation("新增字段信息")
//    @PreAuthorize("@ss.hasPermi('manage:field:add')")
    @Log(title = "字段信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody PcafsFieldAddBo bo) {
        return toAjax(iPcafsFieldService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改字段信息
     */
    @ApiOperation("修改字段信息")
//    @PreAuthorize("@ss.hasPermi('manage:field:edit')")
    @Log(title = "字段信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody PcafsFieldEditBo bo) {
        return toAjax(iPcafsFieldService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除字段信息
     */
    @ApiOperation("删除字段信息")
//    @PreAuthorize("@ss.hasPermi('manage:field:remove')")
    @Log(title = "字段信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Integer[] ids) {
        return toAjax(iPcafsFieldService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
